package com.tg.data.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.tange.base.toolkit.StringUtils;
import com.tg.data.bean.DeviceItem;

import java.util.ArrayList;
import java.util.List;

public class DeviceTypeStorage {

    private static final String SP_FILE_NAME = "TAN_GE_ALL_DEVICE_TYPE";
    private static final String SP_KEY_DEVICE_UUID_ = "SP_KEY_DEVICE_UUID_";
    private static final String SP_KEY_DEVICE_HAS_AI_ = "SP_KEY_DEVICE_HAS_AI_";
    private static final String SP_KEY_DEVICE_IS_SHARE_ = "SP_KEY_DEVICE_IS_SHARE_";
    private static final String SP_KEY_DEVICE_ROTATION_ = "SP_KEY_DEVICE_ROTATION_";
    private static final String SP_KEY_DEVICE_ATTRS = "SP_KEY_DEVICE_ATTRS_";

    private static final String SP_KEY_ALL_DEVICES_UUID = "SP_KEY_ALL_DEVICES_UUID_";
    private static final String SP_KEY_ALL_DEVICES_UUID_SPLIT = "=";

    private static final String SP_KEY_ORDER_URL_ = "SP_KEY_ORDER_URL_";
    private static final String SP_KEY_SHOW_UN_READ_ = "SP_KEY_SHOW_UN_READ_";
    private static final String SP_KEY_UN_READ_COUNT_ = "SP_KEY_UN_READ_COUNT_";
    private static final String SP_KEY_ORDER_ID_ = "SP_KEY_ORDER_ID_";

    public static void saveAllDevice(Context context, List<DeviceItem> deviceItems) {
        if (context == null) {
            return;
        }

        StringBuilder stringBuilder = new StringBuilder();

        if (deviceItems != null && deviceItems.size() > 0) {
            for (DeviceItem item : deviceItems) {
                stringBuilder.append(item.uuid).append(SP_KEY_ALL_DEVICES_UUID_SPLIT);
            }
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KEY_ALL_DEVICES_UUID, stringBuilder.toString());
        editor.commit();
    }

    public static void clearAllDevice(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KEY_ALL_DEVICES_UUID, "");
        editor.commit();
    }

    public static List<String> readAllDevices(Context context) {
        List<String> ret = new ArrayList<>();
        if (context == null) {
            return ret;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        String saved = sharedPreferences.getString(SP_KEY_ALL_DEVICES_UUID, "");
        if (!TextUtils.isEmpty(saved)) {
            String[] split = saved.split(SP_KEY_ALL_DEVICES_UUID_SPLIT);

            if (split != null && split.length > 0) {
                for (String s : split) {
                    if (!TextUtils.isEmpty(s)) {
                        ret.add(s);
                    }
                }
            }
        }
        return ret;
    }

    public static void saveDevice(Context context, DeviceItem item) {
        if (context == null || item == null) {
            return;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KEY_DEVICE_UUID_  + item.uuid, item.device_type);
        editor.putBoolean(SP_KEY_DEVICE_HAS_AI_  + item.uuid, item.ai_server_data != null);
        editor.putBoolean(SP_KEY_DEVICE_IS_SHARE_  + item.uuid, item.isShare());
        editor.putString(SP_KEY_DEVICE_ATTRS  + item.uuid, item.attrs);
        editor.apply();
    }

    public static void saveCustomerInfo(Context context, DeviceItem item){
        if (context == null || item == null) {
            return;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KEY_ORDER_URL_  + item.uuid, item.orderUrl);
        editor.putBoolean(SP_KEY_SHOW_UN_READ_  + item.uuid, item.showUnRead);
        editor.putInt(SP_KEY_UN_READ_COUNT_  + item.uuid, item.unReadCount);
        editor.putInt(SP_KEY_ORDER_ID_  + item.uuid, item.orderId);
        editor.apply();
    }

    public static boolean showCustomerServer(Context context, String uuid){
        SharedPreferences sharedPreferences = context.getSharedPreferences(DeviceTypeStorage.SP_FILE_NAME, Context.MODE_PRIVATE);
        String url =  sharedPreferences.getString(SP_KEY_ORDER_URL_ + uuid, "");
        boolean showUnRead = sharedPreferences.getBoolean(SP_KEY_SHOW_UN_READ_ + uuid, false);
        return showUnRead && !StringUtils.isEmpty(url);
    }

    public static int getUnReadCount(Context context, String uuid){
        SharedPreferences sharedPreferences = context.getSharedPreferences(DeviceTypeStorage.SP_FILE_NAME, Context.MODE_PRIVATE);
        int unRead = sharedPreferences.getInt(SP_KEY_UN_READ_COUNT_ + uuid, 0);
        return unRead;
    }

    public static void setUnReadCount(Context context, String uuid, int count){
        if (context == null) {
            return;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KEY_UN_READ_COUNT_  + uuid, count);
        editor.apply();
    }


    public static String getDeviceAttrs(Context context, String uuid){
        if (context == null || uuid == null) {
            return "";
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(SP_KEY_DEVICE_ATTRS + uuid, "");
    }

    public static void saveDeviceRotation(Context context, DeviceItem item, int rotation) {
        if (context == null || item == null) {
            return;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KEY_DEVICE_ROTATION_  + item.uuid, rotation);
        editor.apply();
    }

    public static int readDeviceRotation(Context context, String uuid) {
        if (context == null || uuid == null) {
            return 0;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(SP_KEY_DEVICE_ROTATION_ + uuid, 0);
    }

    public static String readDeviceType(Context context, String uuid) {
        if (context == null || uuid == null) {
            return "";
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(SP_KEY_DEVICE_UUID_ + uuid, "");
    }

    public static boolean readAI(Context context, String uuid) {
        if (context == null || uuid == null) {
            return false;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(SP_KEY_DEVICE_HAS_AI_ + uuid, false);
    }

    public static boolean readIsShare(Context context, String uuid) {
        if (context == null || uuid == null) {
            return false;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(SP_KEY_DEVICE_IS_SHARE_ + uuid, false);
    }
}
